import torch
import torch.nn as nn
import torch.nn.functional as F

class RoutingAttention(nn.Module):
    def __init__(self, hidden_size, num_iterations=3):
        super(RoutingAttention, self).__init__()
        self.hidden_size = hidden_size
        self.num_iterations = num_iterations

        self.query1 = nn.Linear(hidden_size * 2, hidden_size)
        self.key1 = nn.Linear(hidden_size * 2, hidden_size)
        self.value1 = nn.Linear(hidden_size * 2, hidden_size)

        self.query2 = nn.Linear(hidden_size, hidden_size)
        self.key2 = nn.Linear(hidden_size, hidden_size)
        self.value2 = nn.Linear(hidden_size, hidden_size)

        self.gamma = nn.Parameter(torch.zeros(1))

    def forward(self, hidden_states):
        batch_size, seq_len, _ = hidden_states.size()

        # === First Attention Layer ===
        q1 = self.query1(hidden_states)
        k1 = self.key1(hidden_states)
        v1 = self.value1(hidden_states)

        scores1 = torch.bmm(q1, k1.transpose(1, 2)) / (self.hidden_size ** 0.5)
        attn_weights1 = F.softmax(scores1, dim=-1)
        context1 = torch.bmm(attn_weights1, v1)

        # === Dynamic Routing Attention Layer ===
        b = torch.zeros(batch_size, seq_len, seq_len, device=hidden_states.device)

        for _ in range(self.num_iterations):
            q2 = self.query2(context1)
            k2 = self.key2(context1)
            v2 = self.value2(context1)

            scores2 = torch.bmm(q2, k2.transpose(1, 2)) / (self.hidden_size ** 0.5)
            attn_weights2 = F.softmax(scores2 + b, dim=-1)
            context2 = torch.bmm(attn_weights2, v2)

            # Update routing logits
            b = b + scores2

        # === Output ===
        original_half = hidden_states[:, :, :self.hidden_size]  # 保持与原 SelfAttention 一致
        output = self.gamma * context2 + original_half
        return output


class BiRoutingGRU(nn.Module):
    def __init__(self, input_size, hidden_size, batch_first=True):
        super(BiRoutingGRU, self).__init__()
        self.hidden_size = hidden_size
        self.gru = nn.GRU(input_size, hidden_size, batch_first=batch_first, bidirectional=True)
        self.routing_attention = RoutingAttention(hidden_size)

    def forward(self, x, h0=None):
        out, _ = self.gru(x, h0)
        out = self.routing_attention(out)
        return out


# 示例调用函数
def example_call():
    batch_size = 32
    seq_len = 10
    input_size = 64
    hidden_size = 256

    model = BiRoutingGRU(input_size, hidden_size)
    inputs = torch.randn(batch_size, seq_len, input_size)
    outputs = model(inputs)

    print(f"Input shape: {inputs.shape}")
    print(f"Output shape: {outputs.shape}")

# example_call()  # 取消注释即可运行
